package m.jclecai.com.fragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.RedPkgDetailActivity;
import m.jclecai.com.adapter.RedPkgAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.RedPacketBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.MyLog;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.responseBody.RedpacketBody;
import m.jclecai.com.view.AutoSwipeRefreshLayout;

/**
 * Created by ZD on 2017/6/27.
 */

public class RedPkgFragment extends BaseFragment implements
        SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {

    @BindView(R.id.lv_body)
    ListView lvBody;
    @BindView(R.id.sr_refresh)
    AutoSwipeRefreshLayout sr_refresh;
    List<RedPacketBean> redPcks;
    RedPkgAdapter adapter;
    int flag = 0;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_red;
    }

    @Override
    public void initData() {
        super.initData();
        flag = getArguments() == null ? 0 : getArguments().getInt("flag");
        redPcks = new ArrayList<>();
        sr_refresh.setRefreshing(true);
        sr_refresh.setOnRefreshListener(this);
        adapter = new RedPkgAdapter(redPcks, getActivity(), flag);
        lvBody.setAdapter(adapter);
        lvBody.setOnItemClickListener(this);
        initLoadingLayout(sr_refresh);
        getRedPacket();
    }

    /**
     *
     */
    void getRedPacket() {
        Map<String, String> pair = new HashMap<>();
        pair.put("user", PreferenceUtil.getUserName(getActivity()));//Yes	string	用户名
        pair.put("password", PreferenceUtil.getPassword(getActivity()));//Yes	string	密码
        pair.put("flag", String.valueOf(flag));//
        HttpManager.postAsyn(Constant_Url.MY_REDPACKET, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyLog.i(request.toString());
                if (sr_refresh != null)
                    sr_refresh.setRefreshing(false);
                showInternetOffLayout();
            }

            @Override
            public void onResponse(String response) {
                if (sr_refresh != null)
                    sr_refresh.setRefreshing(false);
                closeLoadingLayout();
                RedpacketBody body = FastOk.get(response, RedpacketBody.class);
                if (body != null && body.getStatus() == 0) {
                    redPcks.clear();
                    redPcks.addAll(body.getRows());
                    adapter.notifyDataSetChanged();
                }
                if (redPcks.size() <= 0)
                    showEmptyLayout();
            }
        }, pair);

    }

    @Override
    public void onRefresh() {
        getRedPacket();
    }

    @Override
    public void OnRetry(String btnMessage) {
        sr_refresh.setRefreshing(true);
        closeLoadingLayout();
        getRedPacket();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RedPacketBean redPacketBean = redPcks.get(position);
        startActivity(new Intent(getActivity(), RedPkgDetailActivity.class).
                putExtra("flag", redPacketBean.getRedpacketid()));
    }
}
